// app/middleware/multer.js
const { existsSync, mkdirSync } = require("fs");
const path = require("path");
const multer = require("multer");

// 确保上传目录存在
const uploadDir = path.join(__dirname, "../../attachment");
if (!existsSync(uploadDir)) {
  mkdirSync(uploadDir, { recursive: true });
}

const storage = multer.diskStorage({
  destination: function (req, file, cb) {
    cb(null, uploadDir);
  },
  filename: function (req, file, cb) {
    // 可以自定义文件名，这里使用原始文件名
    cb(null, file.originalname);
  }
});

const upload = multer({ storage: storage });

module.exports = upload;
